﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIA.HwAccess;
using SIA.Configurators;

namespace SIA.Utility.HwCounter
{
    /// <summary>Gestine di un contatore. Il contatore cambia valore per incremento dei segnali in ingresso </summary>
    public class UpDownHwCounter : HwCounter
    {
        private int _lastIncValue;
        private int _lastDecValue;
        
        public UpDownHwCounter(IHwSignal upHwsignal, IHwSignal downHwsignal)
        {
            

            _upHwSignal = upHwsignal;
            _downHwSignal = downHwsignal;

            if (_upHwSignal is IIntegerSignal)
                _upHwSignal.ValueChange += new SignalEvent(_upHwSignal_ValueChangeEvent);
            else if (upHwsignal is IDigitalSignal)
                ((IDigitalSignal)_upHwSignal).RisePositiveFront += new SignalEvent(upHwsignal_RisePositiveFront);

            if (_downHwSignal is IIntegerSignal)
                _downHwSignal.ValueChange += new SignalEvent(_downHwSignal_ValueChangeEvent);
            else if (upHwsignal is IDigitalSignal)
                ((IDigitalSignal)_downHwSignal).RisePositiveFront += new SignalEvent(downHwsignal_RisePositiveFront);



            int pValue = 0;
            if (_upHwSignal is IIntegerSignal)
                pValue = ((IIntegerSignal)_upHwSignal).IntValue;

            int nValue = 0;
            if (_downHwSignal is IIntegerSignal)
                nValue = ((IIntegerSignal)_downHwSignal).IntValue;

            Value = (pValue - nValue) > 0 ? (pValue - nValue) : 0;
        }

        public UpDownHwCounter(String name, IHwSignal upHwsignal, IHwSignal downHwsignal)
            : this(upHwsignal, downHwsignal)
        {
            this._name = name;
        }



        public UpDownHwCounter(IDictionaryProperty dProperty, IHwSignal upHwsignal, IHwSignal downHwsignal)
            :this(upHwsignal, downHwsignal)
        {
            if (dProperty.ContainsKey("Name"))
                this._name = dProperty["Name"].PropertyValue;
            else
                if (upHwsignal != null)
                    this._name = upHwsignal.Name;
                else
                    this._name = downHwsignal.Name;
            this._group = dProperty["Group"].PropertyValue;
            this._comment = dProperty["Comment"].PropertyValue;
        }

        // Evento Integer UP
        void _upHwSignal_ValueChangeEvent(IHwSignal signal)
        {
            int newValue = ((IIntegerSignal)signal).IntValue;
            int inc = newValue - _lastIncValue;
            if (inc > 0)
                Value = Value + inc;

            _lastIncValue = newValue;
        }
        // Evento Digital UP
        void upHwsignal_RisePositiveFront(IHwSignal signal)
        {
            Value = Value + 1;
        }

        // Evento Integer DOWN
        void _downHwSignal_ValueChangeEvent(IHwSignal signal)
        {
            int newValue = ((IIntegerSignal)signal).IntValue;
            int dec = newValue - _lastDecValue;
            if (dec > 0)
                Value = Value - dec;

            _lastDecValue = newValue;
        }
        // Evento Digital DOWN
        void downHwsignal_RisePositiveFront(IHwSignal signal)
        {
            Value = Value - 1;
        }
    }   
}
